import courseApi from "../../api/chaoxing/courseApi/index";
import userApi from "../../api/chaoxing/userApi/index";
import dbUserApi from "../../api/wechatCloud/dbUserApi/index";
import showToast from "../../utils/toast";
import { refreshConfigs, submiConfigs } from "./throttle";

// pages/config/config.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalAdd: undefined as any,
    courseConfigs: [] as Array<IInPageCourseConfig>,
    courseDetails: [] as CourseType[],
    courseTitleRange: [''],
    currentEditCourseConfig: {} as IInPageCourseConfig,
    edit: false,
    showEdit: false,
    weeks: ["日", "一", "二", "三", "四", "五", "六"],
    rules: {
      name: {
        require: {
          value: true,
          errMsg: ""
        },
        re: {
          value: '^[\u4e00-\u9fa5]{0,}$',
          errMsg: "用户名只能是中文",
        },
      }
    }
  },
  // 点击添加按钮触发
  addCourseConfig({ detail }: { detail:Array<IInPageCourseConfig> }) {
    // 08:00 - 09:00 0 每周日 8点到9点
    this.setData({ courseConfigs: detail })
    // TODO 这里只能在云端访问数据库,如果直接访问,使用者可以抓包跳过逻辑层
    // dbUserApi.setOrUpdateConfigByUsername({})
  },
  // 点击删除按钮触发
  removeCourseConfig(event: WechatMiniprogram.Input) {
    const { courseConfigIdx } = event.currentTarget.dataset
    if (typeof this.data.courseConfigs[courseConfigIdx] != 'undefined') {
      this.data.courseConfigs.splice(courseConfigIdx, 1)
      this.setData({ courseConfigs: this.data.courseConfigs })
    }
  },
  // 点击编辑按钮触发
  editingCourseConfig(event: WechatMiniprogram.Input) {
    const { courseConfigIdx, courseIdx } = event.currentTarget.dataset
    const currentEditCourseConfig = this.data.courseConfigs[courseConfigIdx]
    currentEditCourseConfig.courseIdx = courseIdx
    currentEditCourseConfig.courseConfigIdx = courseConfigIdx
    this.setData({ currentEditCourseConfig: this.data.courseConfigs[courseConfigIdx] })
    this.setData({showEdit:true})
  },
  showAdd(){
    this.setData({showAdd:true})
  },
  submit() {
    submiConfigs(this.data.courseConfigs)
  },
  refresh(){
    refreshConfigs(this.data.courseTitleRange,this)
  },
  editCourseConfigEnd({ detail }: { detail: Array<IInPageCourseConfig> }) {
    this.setData({ courseConfigs: detail })
  },
  openEdit() { this.setData({ edit: !this.data.edit }) },
  onLoad() {
    courseApi.getCoursesLocal().then(res => {
      if (typeof res == 'string') {
        showToast({ title: '课程列表获取失败', icon: "error" })
        return;
      }
      this.setData({ courseDetails: res })
      this.setData({
        courseTitleRange: res.map(course => {
          return course.title;
        })
      })
    })
    this.setData({ username: userApi.getUserRealName()  })
    dbUserApi.getConfigByUsername(userApi.getCurUser()).then(
      res => {
        res.data[0]?.corn_trigger?.map(
          couseConfig => {
            couseConfig.courseIdx = '' + this.data.courseTitleRange.findIndex(courseTitle => courseTitle == couseConfig.course.title)
            return couseConfig
          }
        )
        this.setData({ courseConfigs: res.data[0]?.corn_trigger || [] })
      }
    )
  },
})